package sol.leetcode;

/**
 * Maximum Product of Word Lengths
 */
public class Issue318 {
    public int maxProduct(String[] words) {
        int len = words.length;
        int[] bits = new int[len];//用一个32位的整型记录每一位上的出现的字符
        for (int i = 0; i < len; ++i) {
            for (int j = 0; j < words[i].length(); ++j) {
                bits[i] |= 1 << (words[i].charAt(j) - 'a');
            }
        }

        int max = 0;
        for (int i = 0; i < len; ++i) {
            for (int j = i + 1; j < len; ++j) {
                int product = words[i].length() * words[j].length();
                if ((bits[i] & bits[j]) == 0 && product > max)
                    max = product;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        String[] words = {"a", "ab", "abc", "d", "cd", "bcd", "abcd"};
        System.out.println(new Issue318().maxProduct(words));
    }
}
